package com.dx.mes.service.tool.service;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.tool.service.IMtptoolmuService;
import com.dx.mes.facade.tool.tx.Mtptoolmu.*;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;


@Service("mtptoolmuService")
public class MtptoolmuService implements IMtptoolmuService{
	private LogUtils logUtils;
	private static String TX_NAME = T_MTPTOOLMU;

	@Autowired
	private IBisToolGrDao bisToolGrFao;
	@Autowired
	private IBisToolDao bisToolDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Autowired
	private IRetLineWoDao retLineWoDao; 
	@Autowired
	private IBisMtrlDao bisMtrlDao;
	@Autowired
	private IRetToolMuDao retToolMuDao;
	@Autowired
	private IRetWoBomDao retWoBomDao;
	@Autowired
	private IRetWoMtrlDao retWoMtrlDao;
    @Autowired
    private IBisDataDao bisDataDao;
    
	@Override
	public String subMainProc(String evt_no, String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(MtptoolmuService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(MtptoolmuService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");

		Session session = retToolMuDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = null;

		MtptoolmuI inTrx = new MtptoolmuI();
		MtptoolmuO outTrx = new MtptoolmuO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);


		try {
			inTrx = JacksonUtil.fromJson(strInMsg, MtptoolmuI.class);

			rtnCode = subMainProc2(inTrx, outTrx);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private long subMainProc2(MtptoolmuI inTrx, MtptoolmuO outTrx) {
		long lRc = _NORMAL;
		char actionType = inTrx.getAction_flg().toCharArray()[0];
		switch (actionType) {
			case 'Q': {//查询mount物料信息
				lRc = queryFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'A': {//mount物料信息
				lRc = mountFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'D': {//dismount物料信息
				lRc = unMountFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'T': {//激活、关闭
				lRc = activeMountFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			default: {
				outTrx.setRtn_mesg("Invalide type input: [" + actionType + "]");
				return E_MTPTOOLMU_INVALID_INPUT;
			}
		}
		return lRc;
	}
	private long queryFunc(MtptoolmuI inTrx, MtptoolmuO outTrx) {
		long lRc = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		MtptoolmuIA iary = new MtptoolmuIA();
		
		//检查input信息
		rtnBeen = checkInTrxFunc(inTrx);
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			iary = (MtptoolmuIA) rtnBeen.getObj();
		}
		
		if(Util.isSpaceCheck(iary.getOpe_id())){
			outTrx.setRtn_mesg("站点代码不能为空");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		
		if(Util.isSpaceCheck(iary.getWo_id())){
			outTrx.setRtn_mesg("工单代码不能为空");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		
		String hql = "FROM Ret_tool_mu WHERE line_id_fk ='" + iary.getLine_id() 
				+ "' AND ope_id_fk ='" + iary.getOpe_id() 
				+ "' AND tool_id_fk ='" + iary.getTool_id() 
				+ "' AND wo_id_fk ='" + iary.getWo_id() + "'";
		List<Ret_tool_mu>  ret_tool_mus = retToolMuDao.find(hql);
		List<MtptoolmuOA>  oaryAList = new ArrayList<>();
		for(Ret_tool_mu ret_tool_mu : ret_tool_mus){
			MtptoolmuOA oaryA = new MtptoolmuOA();
			oaryA.setMtrl_prod_id_fk(ret_tool_mu.getId().getMtrl_prod_id_fk());
			oaryA.setMtrl_cate(ret_tool_mu.getId().getMtrl_cate());
			oaryA.setMount_loca(ret_tool_mu.getId().getMount_loca());
			oaryA.setNuse_qty(ret_tool_mu.getMount_qty() - ret_tool_mu.getUse_qty());
			oaryA.setMount_qty(ret_tool_mu.getMount_qty());
			oaryA.setUse_qty(ret_tool_mu.getUse_qty());
			oaryA.setMtrl_mode(ret_tool_mu.getMtrl_mode());
			oaryA.setLine_id_fk(ret_tool_mu.getLine_id_fk());
			oaryA.setTool_id_fk(ret_tool_mu.getId().getTool_id_fk());
			oaryA.setWo_id_fk(ret_tool_mu.getId().getWo_id_fk());
			oaryA.setMtrl_stat(ret_tool_mu.getMtrl_stat());
			oaryA.setOpe_id_fk(ret_tool_mu.getOpe_id_fk());
			oaryA.setMtrl_lot_id(ret_tool_mu.getMtrl_lot_id());

			String hqls ="FROM Ret_wo_mtrl WHERE wo_id_fk='" + ret_tool_mu.getId().getWo_id_fk()
					+ "' AND line_id_fk='" + ret_tool_mu.getLine_id_fk()
					+ "' AND mtrl_cate='" + ret_tool_mu.getId().getMtrl_cate()
					+ "' AND mtrl_prod_id_fk='" + ret_tool_mu.getId().getMtrl_prod_id_fk() + "'";
			List<Ret_wo_mtrl> ret_wo_mtrls = retWoMtrlDao.find(hqls);//显示工单剩余数量
			if(ret_wo_mtrls.size()>0 ){
				oaryA.setWo_qty(String.valueOf(ret_wo_mtrls.get(0).getMtrl_qty()));
			}

			oaryAList.add(oaryA);
		}
		
		//获取mount结果
		List<MtptoolmuOB> oaryBList = new ArrayList<MtptoolmuOB>();
		//获取MTCT设定
			String hql_MTCT = "FROM Bis_data WHERE data_cate ='MTCT' AND ext_2='" + iary.getOpe_id() 
					+ "' AND ext_3='Y'";
	        List<Bis_data> bis_dataList = bisDataDao.find(hql_MTCT);
			for(Bis_data bis_data:bis_dataList){
			MtptoolmuOB oaryB = new MtptoolmuOB();
			oaryB.setMtrl_cate(bis_data.getData_ext());
			oaryB.setMtrl_stat(_NG );
			for(Ret_tool_mu ret_tool_mu:ret_tool_mus){
				if(ret_tool_mu.getId().getMtrl_cate().equals(bis_data.getData_ext())){
					if(_MONT.equals(ret_tool_mu.getMtrl_stat())){
						oaryB.setMtrl_stat(_OK);
					}
				}
			}
			oaryBList.add(oaryB);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setOaryB(oaryBList);
		return lRc;
	}

	/**
	 * Mount物料信息
	 * @param inTrx
	 * @param outTrx
	 * @return
	 */
	private long mountFunc(MtptoolmuI inTrx, MtptoolmuO outTrx) {
		long lRc = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		MtptoolmuIA iary = new MtptoolmuIA();
		Bis_tool_gr bis_tool_gr = new Bis_tool_gr();
		Bis_tool bis_tool = new Bis_tool();
		Bis_ope bis_ope = new Bis_ope();
		Ret_line_wo ret_line_wo = new Ret_line_wo();
		Bis_mtrl bis_mtrl = new Bis_mtrl();
		Ret_wo_bom ret_wo_bom = new Ret_wo_bom();
		Ret_wo_mtrl ret_wo_mtrl = new Ret_wo_mtrl();
		
		//检查input信息
		rtnBeen = checkInTrxFunc(inTrx);
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			iary = (MtptoolmuIA) rtnBeen.getObj();
		}
		
		if(Util.isSpaceCheck(iary.getMtrl_prod_id())){
			outTrx.setRtn_mesg("物料代码不能为空");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		
		if(Util.isSpaceCheck(iary.getMtrl_qty())){
			outTrx.setRtn_mesg("物料数量不能为空");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		
		//获取设备群组信息
		rtnBeen = checkToolGroupFunc(iary.getTool_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_tool_gr = (Bis_tool_gr) rtnBeen.getObj();
		}
		
		//获取设备信息
		rtnBeen = checkToolFunc(iary.getTool_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_tool = (Bis_tool) rtnBeen.getObj();
		}
		
		//根据设备群组获取站点信息
		rtnBeen = checkOpeFunc(bis_tool_gr.getId().getToolg_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_ope = (Bis_ope) rtnBeen.getObj();
		}
		
		//检查线别、站点、工单设定
		rtnBeen = checkLineWoFunc(inTrx, bis_tool, bis_ope);
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			ret_line_wo = (Ret_line_wo) rtnBeen.getObj();
		}
		
		//检查物料基础信息
		rtnBeen = checkMtrlFunc(iary.getMtrl_prod_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_mtrl = (Bis_mtrl) rtnBeen.getObj();
		}
		
		//检查工单Bom是否有物料
		rtnBeen = checkWoBomFunc(bis_mtrl.getMtrl_cate(), ret_line_wo.getWo_id_fk(),
				bis_mtrl.getMtrl_prod_id(), bis_ope.getId().getOpe_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			ret_wo_bom = (Ret_wo_bom) rtnBeen.getObj();
		}
		
		//检查线别、工单是否领料
		rtnBeen = checkWoMtrlFunc(bis_tool.getBay_id(),ret_line_wo.getWo_id_fk(),
				bis_mtrl.getMtrl_cate(), bis_mtrl.getMtrl_prod_id(),
				iary.getMtrl_lot_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			ret_wo_mtrl = (Ret_wo_mtrl) rtnBeen.getObj();
		}
		
		//同一个设备，同一工单、一个端口，只能mount一种物料
		rtnBeen = checkMtrlPortFunc(iary.getTool_id(), bis_mtrl.getMtrl_cate(), 
				bis_mtrl.getMtrl_prod_id(), ret_line_wo.getWo_id_fk(), 
				iary.getMtrl_port(), iary.getMtrl_lot_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}
		
		//Mount物料信息
		if(Double.valueOf(iary.getMtrl_qty()) > ret_wo_mtrl.getMtrl_qty()){
            outTrx.setRtn_mesg("Mount物料数量[" + Double.valueOf(iary.getMtrl_qty()) 
            		+ "]不能超过工单剩余数量[" + ret_wo_mtrl.getMtrl_qty() + "]");
            return E_MTPTOOLMU_NOT_QTY;
		}
		Ret_tool_muId tool_muId = new Ret_tool_muId();
		tool_muId.setTool_id_fk(iary.getTool_id());
		tool_muId.setMtrl_cate(bis_mtrl.getMtrl_cate());
		tool_muId.setMtrl_prod_id_fk(bis_mtrl.getMtrl_prod_id());
		tool_muId.setWo_id_fk(ret_line_wo.getWo_id_fk());
		tool_muId.setMount_loca(iary.getMtrl_port());

		Ret_tool_mu ret_tool_mu = retToolMuDao.getWithLock(tool_muId, LockMode.UPGRADE);
		if(ret_tool_mu != null){
			ret_tool_mu.setMtrl_lot_id(iary.getMtrl_lot_id());
			ret_tool_mu.setMount_qty(ret_tool_mu.getMount_qty() + Double.valueOf(iary.getMtrl_qty()));
			ret_tool_mu.setMtrl_stat(MTRL_MOUNT);
			ret_tool_mu.setEvt_usr(inTrx.getEvt_usr());
			ret_tool_mu.setEvt_timestamp(Util.getCurrentTimestamp());
		}else{
			ret_tool_mu = new Ret_tool_mu();
			ret_tool_mu.setId(tool_muId);
			ret_tool_mu.setMtrl_lot_id(iary.getMtrl_lot_id());
			ret_tool_mu.setMtrl_unit(bis_mtrl.getMtrl_unit());
			ret_tool_mu.setMount_qty(Double.valueOf(iary.getMtrl_qty()));
			ret_tool_mu.setUse_qty(0.0);
			ret_tool_mu.setMtrl_stat(MTRL_MOUNT);
			ret_tool_mu.setLine_id_fk(bis_tool.getBay_id());
			ret_tool_mu.setOpe_id_fk(bis_ope.getId().getOpe_id());
			ret_tool_mu.setMtrl_mode(_NO);
			ret_tool_mu.setEvt_usr(inTrx.getEvt_usr());
			ret_tool_mu.setEvt_timestamp(Util.getCurrentTimestamp());
		}
		retToolMuDao.saveOrUpdate(ret_tool_mu);
		
		//更新工单领料信息
		ret_wo_mtrl.setMtrl_qty(ret_wo_mtrl.getMtrl_qty() - Double.valueOf(iary.getMtrl_qty()));
		ret_wo_mtrl.setEvt_qty(Double.valueOf(iary.getMtrl_qty()));
		ret_wo_mtrl.setEvt_cate(EVT_MOUNT);
		ret_wo_mtrl.setEvt_usr(inTrx.getEvt_usr());
		ret_wo_mtrl.setEvt_timestamp(Util.getCurrentTimestamp());
        retWoMtrlDao.update(ret_wo_mtrl);
		return lRc;
	}
	
	/**
	 * Dismount物料信息
	 * @param inTrx
	 * @param outTrx
	 * @return
	 */
	private long unMountFunc(MtptoolmuI inTrx, MtptoolmuO outTrx) {
		long lRc = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		MtptoolmuIA iary = new MtptoolmuIA();
		Bis_tool_gr bis_tool_gr = new Bis_tool_gr();
		Bis_tool bis_tool = new Bis_tool();
		Bis_ope bis_ope = new Bis_ope();
		Ret_line_wo ret_line_wo = new Ret_line_wo();
		Bis_mtrl bis_mtrl = new Bis_mtrl();
		Ret_wo_bom ret_wo_bom = new Ret_wo_bom();
		Ret_wo_mtrl ret_wo_mtrl = new Ret_wo_mtrl();
		//检查input信息
		rtnBeen = checkInTrxFunc(inTrx);
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			iary = (MtptoolmuIA) rtnBeen.getObj();
		}
		
		if(Util.isSpaceCheck(iary.getMtrl_prod_id())){
			outTrx.setRtn_mesg("物料代码不能为空");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		
		if(Util.isSpaceCheck(iary.getMtrl_qty())){
			outTrx.setRtn_mesg("物料数量不能为空");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		
		//获取设备群组信息
		rtnBeen = checkToolGroupFunc(iary.getTool_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_tool_gr = (Bis_tool_gr) rtnBeen.getObj();
		}
		
		//获取设备信息
		rtnBeen = checkToolFunc(iary.getTool_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_tool = (Bis_tool) rtnBeen.getObj();
		}
		
		//根据设备群组获取站点信息
		rtnBeen = checkOpeFunc(bis_tool_gr.getId().getToolg_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_ope = (Bis_ope) rtnBeen.getObj();
		}
		
		//检查线别、站点、工单设定
		rtnBeen = checkLineWoFunc(inTrx, bis_tool, bis_ope);
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			ret_line_wo = (Ret_line_wo) rtnBeen.getObj();
		}
		
		//检查物料基础信息
		rtnBeen = checkMtrlFunc(iary.getMtrl_prod_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			bis_mtrl = (Bis_mtrl) rtnBeen.getObj();
		}
		
		//检查工单Bom是否有物料
		rtnBeen = checkWoBomFunc(bis_mtrl.getMtrl_cate(), ret_line_wo.getWo_id_fk(),
				bis_mtrl.getMtrl_prod_id(), bis_ope.getId().getOpe_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			ret_wo_bom = (Ret_wo_bom) rtnBeen.getObj();
		}
		
		//检查线别、工单是否领料
		rtnBeen = checkWoMtrlFunc(bis_tool.getBay_id(),ret_line_wo.getWo_id_fk(),
				bis_mtrl.getMtrl_cate(), bis_mtrl.getMtrl_prod_id(),
				iary.getMtrl_lot_id());
		if(_NORMAL != rtnBeen.getlRc()){
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return rtnBeen.getlRc();
		}else{
			ret_wo_mtrl = (Ret_wo_mtrl) rtnBeen.getObj();
		}
		
		//Dismount物料信息
		String hql = "FROM Ret_tool_mu WHERE tool_id_fk='" + iary.getTool_id()
				+ "' AND mtrl_cate='" + bis_mtrl.getMtrl_cate()
				+ "' AND mtrl_prod_id_fk='" + iary.getMtrl_prod_id()
				+ "' AND wo_id_fk='" + ret_line_wo.getWo_id_fk() 
				+ "' AND mount_loca='" + iary.getMtrl_port() 
				+ "' AND mtrl_stat='" + MTRL_MOUNT + "'";
        if(!Util.isSpaceCheck(iary.getMtrl_lot_id())){
            hql = hql + " AND mtrl_lot_id ='" + iary.getMtrl_lot_id() + "'";
        }else{
        	hql = hql + " AND mtrl_lot_id is null";
        }
		List<Ret_tool_mu> ret_tool_mus = retToolMuDao.find(hql);
		if(ret_tool_mus.isEmpty()){
			outTrx.setRtn_mesg("未找到设备代码[" + iary.getTool_id()
					+ "],物料种类[" + bis_mtrl.getMtrl_cate()
					+ "],物料代码[" + iary.getMtrl_prod_id()
					+ "],物料批次[" + iary.getMtrl_lot_id()
					+ "],工单代码[" + ret_line_wo.getWo_id_fk()
					+ "],上料端口[" + iary.getMtrl_port()
					+ "]的装配信息");
			return E_RET_TOOL_MU + E_READ_NOT_FOUND;
		}
		
		Ret_tool_mu ret_tool_mu = ret_tool_mus.get(0);
		if(_YES.equals(ret_tool_mu.getMtrl_mode())){
			outTrx.setRtn_mesg("设备 [" + iary.getTool_id()
					+ "]装配物料代码[" + iary.getMtrl_prod_id()
					+ "]的物料正在使用中，不能解除装配");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		if(Double.valueOf(iary.getMtrl_qty()) > (ret_tool_mu.getMount_qty()-ret_tool_mu.getUse_qty())){
			outTrx.setRtn_mesg("解除装配的物料数量不能超过剩余数量");
			return E_MTPTOOLMU_NOT_QTY;
		}
		
		if(Double.doubleToLongBits(Double.valueOf(iary.getMtrl_qty())) == 
				Double.doubleToLongBits(ret_tool_mu.getMount_qty()-ret_tool_mu.getUse_qty())){
			ret_tool_mu.setMtrl_stat(MTRL_UNMONT);
		}
		ret_tool_mu.setMount_qty(ret_tool_mu.getMount_qty()-Double.valueOf(iary.getMtrl_qty()));
		ret_tool_mu.setEvt_usr(inTrx.getEvt_usr());
		ret_tool_mu.setEvt_timestamp(Util.getCurrentTimestamp());
		retToolMuDao.update(ret_tool_mu);
		
		//更新工单领料信息
		ret_wo_mtrl.setMtrl_qty(ret_wo_mtrl.getMtrl_qty() + Double.valueOf(iary.getMtrl_qty()));
		ret_wo_mtrl.setEvt_qty(Double.valueOf(iary.getMtrl_qty()));
		ret_wo_mtrl.setEvt_cate(EVT_DISMOUNT);
		ret_wo_mtrl.setEvt_usr(inTrx.getEvt_usr());
		ret_wo_mtrl.setEvt_timestamp(Util.getCurrentTimestamp());
        retWoMtrlDao.update(ret_wo_mtrl);
        
		return lRc;
	}

	/**
	 * 激活、关闭
	 * @param inTrx
	 * @param outTrx
	 * @return
	 */
	private long activeMountFunc(MtptoolmuI inTrx, MtptoolmuO outTrx) {
		long lRc = _NORMAL;
		if(inTrx.getIary().size() == 0){
			outTrx.setRtn_mesg("查询数据不能为空");
			return E_MTPTOOLMU_INVALID_INPUT;
		}
		
		MtptoolmuIA iary = inTrx.getIary().get(0);
		Ret_tool_muId tool_muId = new Ret_tool_muId();
		tool_muId.setTool_id_fk(iary.getTool_id());
		tool_muId.setMtrl_cate(iary.getMtrl_cate());
		tool_muId.setMtrl_prod_id_fk(iary.getMtrl_prod_id());
		tool_muId.setWo_id_fk(iary.getWo_id());
		tool_muId.setMount_loca(iary.getMtrl_port());
		Ret_tool_mu ret_tool_mu = retToolMuDao.getWithLock(tool_muId, LockMode.UPGRADE);
		if(ret_tool_mu == null){
			outTrx.setRtn_mesg("未找到设备代码[" + iary.getTool_id()
					+ "],物料种类[" + iary.getMtrl_cate()
					+ "],物料代码[" + iary.getMtrl_prod_id()
					+ "],工单代码[" + iary.getWo_id()
					+ "],上料端口[" + iary.getMtrl_port()
					+ "]的装配信息");
			return E_RET_TOOL_MU + E_READ_NOT_FOUND;
		}
		
		
		if(_YES.equals(ret_tool_mu.getMtrl_mode())){//关闭
			ret_tool_mu.setMtrl_mode(_NO);
		}else{//激活
			//检查装配状态，解除装配装配无法激活
			if(MTRL_UNMONT.equals(ret_tool_mu.getMtrl_stat())){
				outTrx.setRtn_mesg("设备代码[" + iary.getTool_id()
						+ "],物料种类[" + iary.getMtrl_cate()
						+ "],物料代码[" + iary.getMtrl_prod_id()
						+ "],工单代码[" + iary.getWo_id()
						+ "],上料端口[" + iary.getMtrl_port()
						+ "]的装配信息已经解除装配，无法激活");
				return E_MTPTOOLMU_INVALID_MOUNT_STAT;
			}
			//检查同一设备、同一工单、同一物料类型是否有其他激活的信息
			String hql = "FROM Ret_tool_mu WHERE tool_id_fk='" + iary.getTool_id()
					+ "' AND mtrl_cate='" + iary.getMtrl_cate()
					+ "' AND wo_id_fk='" + iary.getWo_id() 
					+ "' AND mtrl_mode='" + _YES + "'";
			List<Ret_tool_mu> ret_tool_mus = retToolMuDao.find(hql);
			if(ret_tool_mus.size() > 0){
				outTrx.setRtn_mesg("设备代码[" + iary.getTool_id()
						+ "],工单代码[" + iary.getWo_id() 
						+ "]同一物料种类[" + iary.getMtrl_cate() + "]已经激活，请先关闭后再激活");
				return E_MTPTOOLMU_VALID_MTRL_MODE;
			}
			ret_tool_mu.setMtrl_mode(_YES);
		}
		ret_tool_mu.setEvt_timestamp(Util.getCurrentTimestamp());
		ret_tool_mu.setEvt_usr(inTrx.getEvt_usr());
		retToolMuDao.update(ret_tool_mu);

		return lRc;
	}
	
	private SimpleRtnBeen checkInTrxFunc(MtptoolmuI inTrx) {
		if(inTrx.getIary().size() == 0){
			return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_INPUT, "查询数据不能为空");
		}
		MtptoolmuIA iary = inTrx.getIary().get(0);
		if(Util.isSpaceCheck(iary.getLine_id())){
			return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_INPUT, "线别代码不能为空");
		}
		
		if(Util.isSpaceCheck(iary.getTool_id())){
			return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_INPUT, "设备代码不能为空");
		}
		return new SimpleRtnBeen(iary);
	}
	
	/**
	 * 获取设备群组信息
	 * @param tool_id
	 * @return
	 */
	private SimpleRtnBeen checkToolGroupFunc(String tool_id){
		List<Bis_tool_gr> bis_tool_gr_list = bisToolGrFao.find("FROM Bis_tool_gr WHERE tool_id_fk='" + tool_id + "'");
		if(bis_tool_gr_list.size() > 1){
			return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_TOOL_INFO, 
					"错误的设备群组信息，设备[" + tool_id + "]只允许绑定一个设备群组");
		}else if(bis_tool_gr_list.isEmpty()){
			return new SimpleRtnBeen(E_BIS_TOOL_GR + E_READ_NOT_FOUND, 
					"未找到设备[" + tool_id + "]绑定的设备群组信息");
		}
		return new SimpleRtnBeen(bis_tool_gr_list.get(0));
	}
	
	/**
	 * 获取设备信息
	 * @param tool_id
	 * @return
	 */
	private SimpleRtnBeen checkToolFunc(String tool_id){
		Bis_tool bis_tool = bisToolDao.get(tool_id);
		if(bis_tool == null){
			return new SimpleRtnBeen(E_BIS_TOOL + E_READ_NOT_FOUND, 
					"未找到设备[" + tool_id + "]信息");
		}
		return new SimpleRtnBeen(bis_tool);
	}
	
	/**
	 * 根据设备群组获取站点信息
	 * @param toolg_id
	 * @return
	 */
	private SimpleRtnBeen checkOpeFunc(String toolg_id){
		List<Bis_ope> bis_ope_list = bisOpeDao.find("FROM Bis_ope WHERE toolg_id_fk='" + toolg_id + "'");
		if(bis_ope_list.size() > 1){
			return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_OPE_INFO,
					"错误的站点信息，设备群组[" + toolg_id + "]只允许绑定一个站点");
		}else if(bis_ope_list.isEmpty()){
			return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND, 
					"未找到设备群组[" + toolg_id + "]绑定的站点信息");
		}
		return new SimpleRtnBeen(bis_ope_list.get(0));
	}
	
	/**
	 * 检查线别、站点、工单设定
	 * @param inTrx
	 * @param bis_tool
	 * @param bis_ope
	 * @return
	 */
	private SimpleRtnBeen checkLineWoFunc(MtptoolmuI inTrx, Bis_tool bis_tool, Bis_ope bis_ope){
		MtptoolmuIA iary = inTrx.getIary().get(0);
		List<Ret_line_wo> ret_line_wo_list = new ArrayList<Ret_line_wo>();
		if(_YES.equals(inTrx.getOpi_ope_flg())){
			if(Util.isSpaceCheck(iary.getWo_id())){
				return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_INPUT, "工单代码不能为空");
			}
			ret_line_wo_list = retLineWoDao.find("FROM Ret_line_wo WHERE line_id_fk='" + bis_tool.getBay_id() 
					+ "' AND ope_id_fk='" + bis_ope.getId().getOpe_id()
					+ "' AND wo_id_fk='" + iary.getWo_id()
					+ "' AND valid_flg='Y'");
		}else{
			ret_line_wo_list = retLineWoDao.find("FROM Ret_line_wo WHERE line_id_fk='" + bis_tool.getBay_id() 
					+ "' AND ope_id_fk='" + bis_ope.getId().getOpe_id()
					+ "' AND valid_flg='Y'");
		}
		
		if(ret_line_wo_list.size() > 1){
			return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_LINE_WO_INFO,
					"错误的线别站点工单设定信息，线别[" + bis_tool.getBay_id() 
					+ "],站点[" + bis_ope.getId().getOpe_id() + "]只允许激活一个工单");
		}else if(ret_line_wo_list.isEmpty()){
			return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND, 
					"未找到线别[" + bis_tool.getBay_id() 
					+ "],站点[" + bis_ope.getId().getOpe_id() + "]激活的工单信息");
		}
		return new SimpleRtnBeen(ret_line_wo_list.get(0));
	}
	
	/**
	 * 检查物料基础信息是否维护
	 * @param mtrl_prod_id
	 * @return
	 */
	private SimpleRtnBeen checkMtrlFunc(String mtrl_prod_id) {
		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id);
		if(bis_mtrl == null){
			return new SimpleRtnBeen(E_BIS_MTRL + E_READ_NOT_FOUND, "未找到物料代码[" + mtrl_prod_id + "]的基础信息");
		}
		return new SimpleRtnBeen(bis_mtrl);
	}
	
	/**
	 * 检查工单是包含该物料
	 * @param mtrl_cate
	 * @param wo_id_fk
	 * @param mtrl_prod_id
	 * @return
	 */
	private SimpleRtnBeen checkWoBomFunc(String mtrl_cate,String wo_id_fk,
			String mtrl_prod_id, String ope_id) {
		Ret_wo_bomId ret_wo_bomId = new Ret_wo_bomId();
		ret_wo_bomId.setMtrl_cate(mtrl_cate);
		ret_wo_bomId.setWo_id_fk(wo_id_fk);
		ret_wo_bomId.setMtrl_prod_id_fk(mtrl_prod_id);
		Ret_wo_bom ret_wo_bom = retWoBomDao.get(ret_wo_bomId);
		if(ret_wo_bom == null){
			return new SimpleRtnBeen(E_BIS_MTRL + E_READ_NOT_FOUND, 
					"工单代码[" +wo_id_fk+ "]不包含物料种类[" + mtrl_cate + "],物料代码[" + mtrl_prod_id + "]的物料信息");
		}/*else if(!ope_id.equalsIgnoreCase(ret_wo_bom.getOpe_id_fk())){//检查物料使用站点
			return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_OPE, 
					"物料种类[" + mtrl_cate + "],物料代码[" + mtrl_prod_id + "]使用站点不正确");
		}*/
		return new SimpleRtnBeen(ret_wo_bom);
	}
	
	/**
	 * 检查工单是否领料
	 * @param wo_id
	 * @param mtrl_prod_id
	 * @param mtrl_lot_id
	 * @return
	 */
	private SimpleRtnBeen checkWoMtrlFunc(String line_id, String wo_id, 
			String mtrl_cate, String mtrl_prod_id, 
			String mtrl_lot_id) {
		String hql ="FROM Ret_wo_mtrl WHERE wo_id_fk='" + wo_id
				+ "' AND line_id_fk='" + line_id
				+ "' AND mtrl_cate='" + mtrl_cate
				+ "' AND mtrl_prod_id_fk='" + mtrl_prod_id + "'";
		
        if(!Util.isSpaceCheck(mtrl_lot_id)){
            hql = hql + " AND mtrl_lot_id ='" + mtrl_lot_id + "'";
        }else{
        	hql = hql + " AND mtrl_lot_id is null";
        }
        
		List<Ret_wo_mtrl> ret_wo_mtrls = retWoMtrlDao.find(hql);
		if(ret_wo_mtrls.isEmpty()){
			return new SimpleRtnBeen(E_BIS_MTRL + E_READ_NOT_FOUND, 
					"未找到线别代码[" + line_id + "],工单代码[" + wo_id 
					+ "],物料种类[" + mtrl_cate 
					+ "],物料代码[" + mtrl_prod_id 
					+ "],批次代码[" + mtrl_lot_id 
					+ "]的领料信息");
		}
		return new SimpleRtnBeen(ret_wo_mtrls.get(0));
	}
	
	/**
	 * 同一个工单，同一个设备，同一种物料，同一个端口只能mount一种物料
	 * @param wo_id
	 * @param mtrl_cate
	 * @param tool_id_fk
	 * @param mtrl_port
	 * @param mtrl_lot_id
	 * @return
	 */
	private SimpleRtnBeen checkMtrlPortFunc(String tool_id_fk, String mtrl_cate, 
			String mtrl_prod_id, String wo_id, 
			String mtrl_port,String mtrl_lot_id) {
		String hql = "FROM Ret_tool_mu WHERE tool_id_fk='" + tool_id_fk
				+ "' AND mtrl_cate='" + mtrl_cate
				+ "' AND mtrl_prod_id_fk='" + mtrl_prod_id
				+ "' AND wo_id_fk='" + wo_id 
				+ "' AND mount_loca='" + mtrl_port 
				+ "' AND mtrl_stat='" + MTRL_MOUNT + "'";
        if(!Util.isSpaceCheck(mtrl_lot_id)){
            hql = hql + " AND mtrl_lot_id ='" + mtrl_lot_id + "'";
        }else{
        	hql = hql + " AND mtrl_lot_id is null";
        }
		List<Ret_tool_mu> ret_tool_mus = retToolMuDao.find(hql);
		if(ret_tool_mus.isEmpty()){
			hql = "FROM Ret_tool_mu WHERE tool_id_fk='" + tool_id_fk
					+ "' AND mtrl_cate='" + mtrl_cate
					+ "' AND wo_id_fk='" + wo_id 
					+ "' AND mount_loca='" + mtrl_port 
					+ "' AND mtrl_stat='" + MTRL_MOUNT + "'";
			ret_tool_mus = new ArrayList<Ret_tool_mu>();
			ret_tool_mus = retToolMuDao.find(hql);
			if(ret_tool_mus.size() > 0){
				return new SimpleRtnBeen(E_MTPTOOLMU_INVALID_MOUNT_STAT , 
						"设备代码[" + tool_id_fk + "],上料端口[" + mtrl_port + "]已经装配物料种类[" + mtrl_cate + "],请先解除装配后重新装配");
			}
		}

		return new SimpleRtnBeen(_NORMAL);
	}
}
